//获取当前系统日期和时间
var util = require('../../utils/util.js');
Page({
  data: {
    array:['李腊妹', '沈仙招', '张友军'], //普通选择器数据
    nowTime:0,
    index: 0, //普通选择器值索引
    lostmanAge: '', //老人年龄
    lostmanIDCard:'', //老人身份证号码
    lostmanId:'', //走失任务id
    lostmanRemark:'', //走失老人描述
    lostmanSex:'', //老人性别
    manChecked:false, //男的选项默认不选
    womanChecked:false, //女的选项框默认不选
  },
  bandleChange:function(e){
    this.setData({
      lostmanSex:e.detail.value
    })
    console.log("输入的性别为：",this.data.lostmanSex);
  },
  /**
   * 获取当前系统时间
   */
  getTime:function(){
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var currenTime =date.getFullYear();
    this.setData({  // 再通过setData更改Page()里面的lostTime，动态更新页面的数据
      nowTime: currenTime
    });
    console.log("当前年份：",this.data.nowTime)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getTime(); // 获取当前系统时间作为报案时间
    var lostmanId_get = wx.getStorageSync('lostmanId');//wx.getStorageSync(key)，获取本地缓存
    var taskId_get = wx.getStorageSync('taskId');//wx.getStorageSync(key)，获取本地缓存
    this.setData({
      lostmanId:lostmanId_get,
      taskId:taskId_get
    })
    console.log("当前老人的lostmanId为：",lostmanId_get)
    console.log("taskId",taskId_get)
  },
  /**
    * 任务下拉框进行选择
    */
   changeValue(e) {
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 获取老人年龄
   */
  lostmanAge_put:function(e){
    var that = this;
    var age_get = e.detail.value;
    if(age_get >= 60 && age_get < 110){
      that.setData({ 
        lostmanAge: age_get
      }); 
      console.log("年龄输入正确：",age_get)
    }else{
      console.log("年龄输入错误：",age_get)
      wx.showToast({
        title: "年龄需大于60岁",
        image: '../../images/icon/jinggao.png'
      })
    }
  },
  /**
   * 身份证号合法性验证
   * 支持15位和18位身份证号
   * 支持地址编码、出生日期、校验位验证
   */
  checkIDCard:function(code){
    var ts = this;
    console.log("身份证号输入为：",code)
    var city = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 " };
    var tip = "";
    var pass = true;
    var reg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/;
    if (!code || !code.match(reg)) {
        tip = "身份证号格式错误";
        pass = false;
      }else if (!city[code.substr(0, 2)]) {
        tip = "地址编码错误";
        pass = false;
      }else {
        //18位身份证需要验证最后一位校验位
        if (code.length == 18) {
          code = code.split('');
          //∑(ai×Wi)(mod 11)
          //加权因子
          var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
          //校验位
          var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
          var sum = 0;
          var ai = 0;
          var wi = 0;
          for (var i = 0; i < 17; i++) {
            ai = code[i];
            wi = factor[i];
            sum += ai * wi;
          }
          var last = parity[sum % 11];
          if (parity[sum % 11] != code[17]) {
            tip = "校验位错误";
            pass = false;
          }
        }
      }
    console.log("身份证校验：",pass)
    console.log("身份证核验提示tip：",tip);
    if(!pass){
      wx.showToast({
        title: tip,
        image: '../../images/icon/jinggao.png'
      })
    }
    return pass
  },
  /**
   * 获取身份证输入
   */
  lostmanIDCard_put:function(e){
    var that = this;
    var code = e.detail.value;
    var pass = this.checkIDCard(code)
    if (pass) { 
      that.setData({ 
        lostmanIDCard: e.detail.value 
      }); 
      console.log("身份证输入正确：",that.lostmanIDCard)
      var age_get = that.data.nowTime - e.detail.value.substring(6, 10)
      if(age_get >= 60 && age_get < 110){
        that.setData({ 
          lostmanAge: age_get
        }); 
        console.log("身份证获取到的年龄检测正确：",that.data.lostmanAge)
      }else{
        console.log("身份证获取到的年龄检测不符合年龄要求：",age_get)
        wx.showToast({
          title: "年龄需大于60岁",
          image: '../../images/icon/jinggao.png'
        })
      }
      console.log("系统获取到的身份证年龄：",that.data.lostmanAge)
    }
    //获取性别
    if (parseInt(code.substr(16, 1)) % 2 == 1) {
      // 男
      that.setData({
        manChecked:true,
        lostmanSex:0
      })
      console.log("从身份证自动获取的性别为：",that.data.lostmanSex)
    } else {
      // 女
      return "女";
    }
  },
  lostmanRemark_put:function(e){
    var lostmanRemark_get = e.detail.value;
    if(lostmanRemark_get.length != 0){
      this.setData({
        lostmanRemark:lostmanRemark_get
      })
    }
    console.log("获取到的老人描述为：",this.data.lostmanRemark)
  },
  
  /**
   * 提交按钮，上传到数据库
   */
  submitBtn:function(){
    var data = {
      lostmanAge : this.data.lostmanAge, //老人年龄
      lostmanIDCard : this.data.lostmanIDCard, //老人身份证号码
      lostmanId : this.data.lostmanId, //走失任务id
      lostmanRemark : this.data.lostmanRemark, //走失老人描述
      lostmanSex : this.data.lostmanSex //老人性别
    }
    wx.showModal({
      title: '提示',
      content: '确认信息无误并提交',
      success: function (res) {
        if (res.cancel) {
          //点击取消,回到主页
        }else{
          // 提交表单数据到服务器
          wx.request({
            url: 'https://www.lanotherl.com/family/perfect',
            data,
            method:"PUT",
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            success: (res) => {
              console.log("完善信息上传到服务器成功",res);
              wx.showModal({
                title: '信息补充成功',
                content: '回到主页面？',
                success: function (res) {
                  if (res.cancel) {
                    //点击取消,回到主页
                  }else{
                    //点击确定，转到信息完善页面
                    wx.navigateTo({
                      url: '/pages/index/index',
                    })
                  }
                }
              })
            },
            fail: function (err) {
              console.log("完善信息上传到服务器成功失败",err);
            },
          }) 
        }
      }
    })
    
  }
})